using System;
using log4net;
using Microsoft.Practices.ServiceLocation;
using NHibernate;
using NHibernate.Context;

namespace SIP.PN.TestDeIntegracion
{
    internal class RequestEmulator : IDisposable
    {
        private readonly ILog logger = LogManager.GetLogger(typeof(RequestEmulator));

        public RequestEmulator()
        {
            ISession session = SessionFactory.OpenSession();

            CurrentSessionContext.Bind(session);
        }

        private static ISessionFactory SessionFactory
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ISessionFactory>("ISessionFactoryWEB");
            }
        }

        public void Dispose()
        {
            ISession session = SessionFactory.GetCurrentSession();

            session.Close();
            logger.Info(string.Format("Cerrando Session{0}", session.GetHashCode()));

            CurrentSessionContext.Unbind(SessionFactory);
        }
    }
}
